/*
 * @Author: Jim <jimjiang0909@gmail.com>
 * @Create time:   2016-08-23 11:40:01
 * @Last Modified by:   Jim
 * @Last Modified time: 2016-10-14 17:13:19
 * @File Name: userList.js
 */

'use strict';

import React, { Component } from 'react';
import {
    StyleSheet,
    View,
    Text,
    ListView,
    RefreshControl,
} from 'react-native';

import User from './user';

export default class userList extends Component {
    constructor(props) {
        super(props);

        this.state = {
            isLoading: true,
            scrollEnabled: true,
            ds: new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 }),
            isRefreshing: false,
            noMoreData: false,
            noData: false,
            loadingMore: false,
            // resItem: this.props.resItem || 
        };
        this._api = this.props.api;
        this._options = this.props.options;
        this._ajax = true;
        this._data = [];
        this._key = 0;
    }

    componentDidMount() {
        this.getUsers();
    }

    render() {
        if (this.state.noData) {
            return (
                <View key={'userList_' + this._api}>
                    {Utils.noData()}
                </View>
            );
        }
        if (this.state.isLoading) {
            return (
                <View key={'userList_' + this._api}>
                    {Utils.loading()}
                </View>
            );
        }
        return (
            <ListView
                style={{width: Utils.size.width}}
                dataSource={this.state.ds}
                scrollEnabled={this.state.scrollEnabled}
                renderRow={ this.renderRow.bind(this) }
                initialListSize={8}
                pageSize={8}
                onEndReachedThreshold={80}
                onEndReached={ this.onEndReached.bind(this) }
                renderFooter={ this.renderFooter.bind(this) }
                refreshControl={
                    <RefreshControl
                        refreshing={this.state.isRefreshing}
                        onRefresh={ this.onRefresh.bind(this) }
                        />
                }
            />   
        )
    }

    renderRow(rowData: Object, sectionID: number, rowID: number, highlightRow: (sectionID: number, rowID: number) => void) {
        return (
            <User navigator={this.props.navigator} rowData={rowData} sectionID={sectionID} rowID={rowID}  allowScroll={(scrollEnabled) => this.allowScroll(scrollEnabled)}  delData={(rowID) => this.delData(rowID)}/>
        )
    }

    onEndReached() {
        this.setState({loadingMore: true});
        this.getUsers();
    }

    getUsers() {
        let tthis = this;
        if (this._ajax) {
            this._ajax = false;
            Share.fetchMain({
                url: this._api,
                data: this._options,
                callback: function (data) {
                    tthis._ajax = true;
                    let list;
                    if(typeof(data.un_see) !== 'undefined') {
                        list = data.un_see.concat(data.had_see);
                    } else if(typeof(data.res) !== 'undefined') {
                        list = data.res;
                    } else {
                        list = data.list;
                    }
                    if (list.length === 0) {
                        if (tthis._options.page_num == 0) {
                            tthis.setState({ noData: true, isLoading: false });
                        } else {
                            tthis.setState({ noMoreData: true });
                        }
                        tthis._ajax = false;
                    } else {
                        // console.log(tthis._options)
                        if(data.hst_id) {
                            tthis._options.hst_id = data.hst_id;
                        }
                        if(typeof(tthis._options.from) !== 'undefined') {
                            tthis._options.from +=  tthis._options.offset;
                        } else {
                            tthis._options.page_num++;
                        }
                        tthis._data = tthis._data.concat(list);
                        let [result, itemIds] = tthis.assemData(tthis._data);
                        tthis.setState({
                            loadingMore: false,
                            isLoading: false,
                            ds: tthis.state.ds.cloneWithRows(result, itemIds)
                        })
                    }
                }
            })
        }
    }
    allowScroll(scrollEnabled) {
        this.setState({scrollEnabled: scrollEnabled});
    }

    assemData(data) {
        let tmp = {}, itemIds = [];
        for( let i in data) {
            tmp[data[i].usr_id] = data[i];
            itemIds.push(data[i].usr_id);
        }
        return [tmp, itemIds];
    }

    delData(rowID){
        this._data.some((item, index) => {
            if (item.usr_id == rowID) {
                delete this._data[index];
            }
        });
        let [result, itemIds] = this.assemData(this._data);
        this.setState({
            ds: this.state.ds.cloneWithRows(result, itemIds)
        })

        console.log(154,this.state.ds);
    }

    onRefresh() {
        // this.setState({ isRefreshing: true });
        // this.getUsers();
    }

    renderFooter() {
        if(this.state.noMoreData) {
            return Utils.noMoreData();
        }
        if(this.state.loadingMore) {
            return Utils.loadingMore()
        }
    }
}